﻿using InteControlSys.SampleService;
using InteControlSys.ViewModel;
using Microsoft.Win32;
using PropertyTools.Wpf;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Xml.Linq;

namespace InteControlSys.ViewModel
{
    public class BucketTransViewModel :NodeViewModel
    {
        public SampleService.SampleClient sp = new SampleClient("WSHttpBinding_ISample");
        public SampleCommon sc = new SampleCommon();

        #region Properties
        private ObservableCollection<BuckeList> bucketList;
        public ObservableCollection<BuckeList> BucketList
        {
            get { return bucketList; }
            set
            {
                bucketList = value;
                NotifyChanged("BucketList");
            }
        }

        private ObservableCollection<BuckeList> pushList;
        public ObservableCollection<BuckeList> PushList
        {
            get { return pushList; }
            set
            {
                pushList = value;
                NotifyChanged("PushList");
            }
        }

        public List<BuckeList> OriginPushList;

        private BuckeList _selectedBucket;
        public BuckeList SelectedBucket
        {
            get { return _selectedBucket; }
            set
            {
                _selectedBucket = value;
                NotifyChanged("SelectedBucket");
            }
        }

        private int _selectedStatusIndex;
        public int SelectedStatusIndex
        {
            get { return _selectedStatusIndex; }
            set
            {
                _selectedStatusIndex = value;
                NotifyChanged("SelectedStatusIndex");
                GetList();
            }
        }

        private string _selectedPosition;
        public string SelectedPosition
        {
            get { return _selectedPosition; }
            set
            {
                _selectedPosition = value;
                NotifyChanged("SelectedPosition");
                GetList();
            }
        }

        //private bool _isSelected = true;

        //public bool IsSelected
        //{
        //    get { return _isSelected; }

        //    set
        //    {
        //        _isSelected = value;
        //        NotifyChanged("IsSelected");
        //    }
        //}

        private string _errorMSG = null;
        public string ErrorMSG
        {
            get { return _errorMSG; }
            set
            {
                _errorMSG = value;
                NotifyChanged("ErrorMSG");
            }
        }

        private string _selectAllName;

        public string SelectAllName
        {
            get { return _selectAllName; }

            set
            {
                _selectAllName = value;
                NotifyChanged("SelectAllName");
            }
        }

        private bool _selectAll;

        public bool SelectAll
        {
            get { return _selectAll; }

            set
            {
                _selectAll = value;
                NotifyChanged("SelectAll");
               // MakeSelectAll();
            }
        }

        #endregion

        public BucketTransViewModel()
        {
            Init();
        }

        public override Uri GetMyIcon()
        {
            return new Uri("../Resources/Images/bucket.png", UriKind.RelativeOrAbsolute);
        }

        public BucketTransViewModel(string name)
        {
            this.Name = name;
            this.Parent = null;
            this.IsExpanded = true;
            this.NodeType = "BucketTransViewModel";
            this.OperatingTruckList = new ObservableCollection<NodeViewModel>();

            Init();
        }

        #region Command

        public void Init()
        {
            GetList();
            _selectAll = false;
            _selectAllName = "全选";
        }

        public void MakeSelectAll()
        {
            if (bucketList != null && _selectAll == false)
            {
                foreach (var item in bucketList)
                {
                    item.IsSelected = true;
                }
                _selectAll = true;
                _selectAllName = "全不选";
            }
            else if (bucketList != null && _selectAll == true)
            {
                foreach (var item in bucketList)
                {
                    item.IsSelected = false;
                }
                _selectAll = false;
                _selectAllName = "全选";
            }
              this.BucketList = bucketList;
              SelectAll = _selectAll;
              SelectAllName = _selectAllName;
        }

        public ICommand SelectOrUnSelectAll
        {
            get
            {
                return new DelegateCommand(() =>
                {
                    MakeSelectAll();
                });
            }
        }

        public void GetList()
        {
            var list = sp.GetUnpushedBucket(SelectedStatusIndex,SelectedPosition==null?null:SelectedPosition.Replace("System.Windows.Controls.ComboBoxItem: ",""));
            bucketList = new ObservableCollection<BuckeList>();
            foreach (var item in list)
            {
                var bi = new BuckeList();
                bi.BucketSampleInfo = item;
                bi.IsSelected = false;
                bucketList.Add(bi);
            }
            this.BucketList = bucketList;
        }

        public ICommand Seal
        {
            get
            {
                return new DelegateCommand(() =>
                {
                    int i = 0;
                    this.ErrorMSG = null;
                    bucketList = this.BucketList;
                    foreach (var item in bucketList)
                    {
                        if (item.IsSelected)
                        {
                            if (item.BucketSampleInfo.大样状态 != 3)
                            {
                                this.ErrorMSG = "只有取样完成的样桶可下达封桶指令!";
                                break;
                            }
                            else
                            {
                                //调用发送封桶指令的接口

                                if (sp.SealBucket(item.BucketSampleInfo.ID)) i++;
                            }
                        }
                    }
                    GetList();
                    if (i > 0) this.ErrorMSG = "已下达" + i.ToString() + "条封桶指令！";
                });
            }
        }


        public ICommand Order
        {
            get
            {
                return new DelegateCommand(() =>
                {
                    
                    this.ErrorMSG = null;
                    bucketList = this.BucketList;
                    int i = bucketList.Count(o => o.BucketSampleInfo.大样状态 == 4||o.BucketSampleInfo.大样状态==5);
                    pushList = new ObservableCollection<BuckeList>();
                    foreach (var item in bucketList)
                    {
                        if (item.IsSelected)
                        {
                            if (item.BucketSampleInfo.大样状态 != 1)
                            {
                                this.ErrorMSG = "只有已封桶的样桶可进行推出排序!";
                            }
                            else
                            {

                                i++;
                                if (i > 8)
                                {
                                    this.ErrorMSG = "一次最多只能推出8个样桶!";
                                    break;
                                }
                                else
                                {
                                    pushList.Add(item);
                                }
                            }
                        }
                    }
                    this.PushList = pushList;
                    OriginPushList = pushList.ToList();
                });
            }
        }


        public ICommand Top
        {
            get
            {
                return new DelegateCommand(() =>
                {
                    int i = 0;
                    this.ErrorMSG = null;
                    pushList = this.PushList;
                    
                    _selectedBucket = this.SelectedBucket;
                    i = pushList.IndexOf(_selectedBucket);
                    pushList.Insert(0, _selectedBucket);
                    pushList.RemoveAt(i + 1);

                    this.PushList = pushList;
                });
            }
        }

        public ICommand Up
        {
            get
            {
                return new DelegateCommand(() =>
                {
                    int i = 0;
                    this.ErrorMSG = null;
                    pushList = this.PushList;

                    _selectedBucket = this.SelectedBucket;
                    i = pushList.IndexOf(_selectedBucket);
                    pushList.Insert(i - 1, _selectedBucket);
                    pushList.RemoveAt(i + 1);

                    this.PushList = pushList;
                });
            }
        }

        public ICommand Down
        {
            get
            {
                return new DelegateCommand(() =>
                {
                    int i = 0;
                    this.ErrorMSG = null;
                    pushList = this.PushList;

                    _selectedBucket = this.SelectedBucket;
                    i = pushList.IndexOf(_selectedBucket);
                    pushList.Insert(i + 2, _selectedBucket);
                    pushList.RemoveAt(i);

                    this.PushList = pushList;
                });
            }
        }

        public ICommand Buttom
        {
            get
            {
                return new DelegateCommand(() =>
                {
                    int i = 0;
                    this.ErrorMSG = null;
                    pushList = this.PushList;

                    _selectedBucket = this.SelectedBucket;
                    i = pushList.IndexOf(_selectedBucket);
                    pushList.Insert(pushList.Count, _selectedBucket);
                    pushList.RemoveAt(i);

                    this.PushList = pushList;
                });
            }
        }

        public ICommand Origin
        {
            get
            {
                return new DelegateCommand(() =>
                {
                    pushList = new ObservableCollection<BuckeList>();
                    foreach (var item in OriginPushList) pushList.Add(item);
                    this.PushList = pushList;
                });
            }
        }

        public ICommand Clear
        {
            get
            {
                return new DelegateCommand(() =>
                {
                    this.PushList.Clear();
                    OriginPushList.Clear();
                    foreach (var item in bucketList)
                    {
                        item.IsSelected = false;
                    }
                    this.BucketList = bucketList;
                });
            }
        }

        public ICommand Push
        {
            get
            {
                return new DelegateCommand(() =>
                {
                    int i = 0;
                    int order = sp.MaxPushOrder();
                    this.ErrorMSG = null;
                    pushList = this.PushList;
                    foreach (var item in pushList)
                    {
                        order = order + 1;

                        //调用推出指令接口
                        if (sp.SetPushOrder(item.BucketSampleInfo.ID, order)) i++;
                    }
                    this.ErrorMSG = "已将" + i.ToString() + "条推出指令下发给密码桶输送系统。";
                    GetList();
                });
            }
        }

        public ICommand PickUp
        {
            get
            {
                return new DelegateCommand(() =>
                {
                    int i = 0;
                    int noInDay = sp.MaxPickNoToday() + 1;
                    this.ErrorMSG = null;
                    var transID = sp.CreateNewPushOrder();
                    var list = bucketList.Where(o => o.BucketSampleInfo.大样状态 == 5).OrderBy(o => o.BucketSampleInfo.推出排序);
                    foreach (var item in list)
                    {
                        if (sp.SetPickUpOrder(item.BucketSampleInfo.ID, transID)) i++;
                    }
                    this.ErrorMSG = "已将" + i.ToString() + "条取密码桶指令下发给智能取样车。";
                    GetList();
                });
            }
        }

        #endregion
    }

    public class BucketOut
    {
        public BucketOut() { }

        public BucketSampleInfo BucketSampleInfo { get; set; }

        public int OrderNo { get; set; }
    }

    public class BuckeList:INPCBase
    {
        public BuckeList() { }

        public BucketSampleInfo BucketSampleInfo { get; set; }

        //public bool IsSelected { get; set; }
        private bool _isSelected = true;

        public bool IsSelected
        {
            get { return _isSelected; }

            set
            {
                _isSelected = value;
                NotifyChanged("IsSelected");
            }
        }
    }
}
